//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Research.DataLayer
{
    public partial class ConnMgrDlg_Presets : UserControl
    {
        public delegate void PresetHandler(IConnMgrInfoParams preset);
        public event PresetHandler OnLoadPreset;
        private List<IConnMgrInfoParams> presets;

        public ConnMgrDlg_Presets()
        {
            InitializeComponent();
        }

        public int Count
        {
            get { return connSaved.Items.Count; }
        }

        public IConnMgrInfoParams GetPresetByID(Guid id)
        {
            if (presets == null || presets.Count == 0)
                return null;

            foreach (IConnMgrInfoParams ds in presets)
            {
                if (ds.ID.Equals(id))
                    return ds;
            }

            return null;
        }

        public IConnMgrInfoParams CurrentPreset
        {
            get
            {
                return (IConnMgrInfoParams)connSaved.SelectedItem;
            }

            set
            {
                connSaved.SelectedItem = value;

                if (OnLoadPreset != null)
                    OnLoadPreset(value);
            }
        }

        internal void LoadPresets(List<IConnMgrInfoParams> presets)
        {
            this.presets = presets;

            connSaved.Items.Clear();

            if (presets != null)
            {
                connSaved.Items.AddRange(presets.ToArray());

                if (connSaved.Items.Count > 0)
                    connSaved.SelectedIndex = 0;
            }

            object obj = connSaved.SelectedItem;

            if (OnLoadPreset != null)
                OnLoadPreset((IConnMgrInfoParams)obj);
        }

        internal void AddPreset(IConnMgrInfoParams newPreset)
        {
            presets.Add(newPreset);
            connSaved.Items.Add(newPreset);
            connSaved.SelectedItem = newPreset;
        }

        internal void DeletePreset()
        {
            IConnMgrInfoParams obj = (IConnMgrInfoParams)connSaved.SelectedItem;

            if (obj != null)
            {
                int curIdx = connSaved.Items.IndexOf(obj);

                presets.Remove(obj);
                connSaved.Items.Remove(obj);

                connSaved.SelectedIndex = (connSaved.Items.Count == 0 ? -1 :
                    (connSaved.Items.Count < curIdx ? curIdx : connSaved.Items.Count - 1));

                // New item selected
                obj = (IConnMgrInfoParams)connSaved.SelectedItem;

                if (OnLoadPreset != null)
                    OnLoadPreset(obj);
            }
        }

        private void connSaved_SelectedIndexChanged(object sender, EventArgs e)
        {
            object obj = connSaved.SelectedItem;

            if (OnLoadPreset != null)
                OnLoadPreset((IConnMgrInfoParams)obj);
        }

        private void connSaved_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = ((IConnMgrInfoParams)e.ListItem).Name;
        }
    }
}
